// pages/carQueryRecord/details/details.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scopeRowReport:{
      balance: 0,
      consumerScore: 0,
      drivingLicenseUrl: "",
      engineNo: "",
      orderAmount: 0,
      orderNo: "",
      orderStatus: null,
      orderType: "",
      originalPrice: 0,
      payTime: "",
      plateNo: "",
      reportOrderNo: "",
      reportType: "",
      reportUrl: "",
      vin: "",
      wxpayAmount: 0
    },
    orderStatus:{
      0:'待支付',
      1:'已支付(待查询)',
      2:'查询中',
      3:'报告已生成',
      4:'退款中',
      5:'已退款',
      6:'退款成功',
      99:'超时关闭',
      98:'已取消',
      97:'购买报告错误',
      96:'查询报告错误'
    },
    reporType:{
      'c300InsuranceReport':'车300碰撞保险记录',
      'c300MtnceReport':'车300保养维修记录',
      'cbsInsuranceReport':'查博士碰撞保险记录',
      'cbsMtnceReport':'查博士保养维修记录'
    },
    orderInfo:{
      orderNo:'',
      orderStatus: 0,
      payInfo:{}
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  doNothing(){},
  onLoad: function (options) {
    this.InitUserStatus();
  },
  // 检查一下登录状态。
  async InitUserStatus(){
      app.showLoading("获取用户信息");
      await app.checkToken();
      this.InitUserInfo();
      wx.hideLoading();
  },
  InitUserInfo(){
      let userInfo = app.globalData.userInfo;
      // 这里再验证一遍权限。
      // console.log('userInfo',userInfo);
      if(userInfo.roleList.indexOf('reportApplyAudit') == -1){
        app.showNoAuthOrNoGrant('您无权操作查看此页面');
        return;
      }else{
        let scopeRowReport = app.globalData.scopeRowReport;
        // let scopeRowReport = {
        //   orderNo: "2112281951000848"
        // }
        if(null == scopeRowReport){
          app.showNoAuthOrNoGrant('缺失重要参数，请重试');
          return;
        };
        this.setData({
          'scopeRowReport.orderNo': scopeRowReport.orderNo
        });
        // 请求数据
        this.reportOrderDetail();
      }
  },
  reportOrderDetail(){
    app.sHttp.reportOrderDetail(this.data.scopeRowReport.orderNo).then(res=>{
      this.setData({
        'scopeRowReport': res.result
      })
    },err=>{
      app.error(err.msg);
    })
  },
  copyText: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  lookReprotInfo(e){
    app.globalData.lookReportInfoUrl = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages/carQueryRecord/detailed/detailed',
    })
  },
  openCustomerServiceChat(){
    app.openCustomerServiceChat();
  },
  // 拉起重新支付。
  reportOrderPayInfo(e){
    let self = this;
    let orderNo = e.currentTarget.dataset.orderno;
    app.sHttp.reportOrderPayInfo(orderNo).then(res=>{
      this.setData({
        'orderInfo.payInfo': res.payInfo,
        'orderInfo.orderNo': orderNo,
      }, () => {
        self.tuneUpPay();
      });
    },err=>{
      app.error(err.msg);
    })
  },
  reportOrderCancel(e){
    let self = this;
    wx.showModal({
      title: '提示',
      content: '您确定要取消当前订单',
      success (res) {
        if (res.confirm) {
          let orderNo = e.currentTarget.dataset.orderno;
          app.sHttp.reportOrderCancel(orderNo).then(res=>{
            app.alert('取消成功,已退还余额与积分');
            let that = self;
            setTimeout(() => {
              that.reportOrderDetail();
            }, 1500);
          },err=>{
            app.error(err.msg);
          })

        }
      }
    })
  },
  tuneUpPay(){
    // 从Data里面拿参数
    let orderInfo = this.data.orderInfo;
    try {
      if ('' == orderInfo.orderNo || undefined == orderInfo.payInfo.paySign) {
        app.alert('支付参数不完整');
        return;
      };
      // 吊起支付。
      let self = this;
      wx.requestPayment({
          timeStamp: orderInfo.payInfo.timeStamp,
          nonceStr: orderInfo.payInfo.nonceStr,
          package: orderInfo.payInfo.package,
          signType: orderInfo.payInfo.signType,
          paySign: orderInfo.payInfo.paySign,
          success (res) {
              // 查询订单完成信息。
              let _this = self;
              app.showLoading('查询订单状态');
              setTimeout(() => {
                  wx.hideLoading();
                  _this.OrderPayResult();
              }, 5000);
          },
          fail (res) {
            self.payOk('支付失败','您已取消支付，可在订单列表中选择继续支付');
          }
      })
    } catch (error) {
      app.alert('支付参数不完整');
    }
  },
  OrderPayResult(){
      let orderInfo = this.data.orderInfo;
      if ('' == orderInfo.orderNo) {
          app.alert('支付参数不完整');
          return;
      };
      app.sHttp.reportOrderPayResult(orderInfo.orderNo).then(res =>{
          let orderStatus = res.orderStatus;
          if (0 == orderStatus) {
              this.payOk('查询结果','请等待几分钟后在订单列表中查看订单状态，如长时间未显示成功状态，请联系客服处理');
              return;
          }else if (1 == orderStatus) {
              this.payOk('提交完成','您当前订单提交完成，请等待报告结果');
              return;
          }else{
            this.payOk('查询结果','请等待几分钟后刷新查看，如长时间未显示成功状态，请联系客服处理');
          }
      },err=>{
        app.error(err.msg);
      })
  },
  payOk(title,content){
    let self = this;
    wx.showModal({
      title: title,
      content: content,
      confirmText: '我知道了',
      showCancel: false,
      success(res) {
          self.reportOrderDetail();
      }
    })
  },
})